package com.gongpingjialoan.utility;

import android.text.TextUtils;

import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.network.NetDataJson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/9.
 */
public class StepMonitor {
    private List<Monitor> monitors = new ArrayList<>();
    private static final int MAXNUM = 10;


    private static StepMonitor instance;

    public static synchronized StepMonitor getInstance() {
        if (instance == null) {
            instance = new StepMonitor();
        }
        return instance;
    }

    public void clear() {
        monitors.clear();
        instance = null;
    }

    public boolean isClose = false;


    /**
     * 埋点
     *
     * @param action
     */
    public void addMonitor(Monitor action) {
        monitors.add(action);
        Utils.LogD("monitors--->" + monitors.size() + "--->" + action.getAction() + "--->" + action.getType() + "--->" + action.getRoll_pages());
        if (monitors.size() >= MAXNUM) {
            sendData();
        }
    }


    /**
     * 添加立即发送
     */
    public void addMonitorSend(Monitor action) {
        monitors.add(action);
        sendData();
    }


    //直接发送数据
    public void sendData() {
        JSONArray accountArray = new JSONArray();
        for (int i = 0; i < monitors.size(); i++) {
            String accountStr = getJsonFromBean(monitors.get(i));
            JSONObject accountObject;
            try {
                accountObject = new JSONObject(accountStr);
                accountArray.put(i, accountObject);
            } catch (Exception e) {
            }
        }
        setpRecord(accountArray);
        monitors.clear();

    }

    private String getJsonFromBean(Monitor action) {
        Map map = new HashMap();
        map.put("type", action.getType());
        map.put("timestamp", action.getTimestamp());
        map.put("action", action.getAction());
        if ("eval_banner".equals(action.getAction())) {
            map.put("id", action.getBannerId());
        }
        if ("scroll".equals(action.getType())) {
            map.put("scroll_pages", action.getRoll_pages());
        }

        if (!TextUtils.isEmpty(action.getPhone()) && !TextUtils.isEmpty(action.getCar_id())) {
            Map extramap = new HashMap();
            extramap.put("phone", action.getPhone());
            extramap.put("car_id", action.getCar_id());
            map.put("extra", extramap);
        }

        if (action.getMap() != null) {
            map.put("extra", action.getMap());
        }

        JSONObject json = new JSONObject(map);
        return json.toString();
    }


    private void setpRecord(JSONArray accountArray) {
        // Toast.makeText(GPJApplication.getInstance(), monitors.toString(), Toast.LENGTH_SHORT).show();
        NetDataJson mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
            @Override
            public void onDataJsonUpdate(JSONObject json) {
                Utils.debug("success");
                if (isClose) {
//                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                if (isClose) {
//                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        });
        mNetDataJson.setUrl("/mobile/api/add-action-record/");
        mNetDataJson.addParam("device_code", GPJApplication.getInstance().getDeviceCode());
        //   mNetDataJson.addParam("device", "android_" + GPJApplication.getInstance().channel);
        mNetDataJson.addParam("app_version", GPJApplication.getInstance().getVersion());
        mNetDataJson.addParam("actions", accountArray.toString());
        mNetDataJson.request("post");
    }

}
